package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.FoundGoodContentBeen;
import cn.xfdzx.android.apps.shop.bean.HomeMarketSelectBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.HomeMarketPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.RoundedFormation;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.ShoppingCartAnimationView;
import cn.xfdzx.android.apps.shop.view.TagTextView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.SHOPID_69_NO;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：发现好货
 */
public class FoundGoodsAdapter extends RecyclerView.Adapter<FoundGoodsAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<FoundGoodContentBeen.DataBean> mList;
    private Activity mContext;
    private int proportion;

    private RoundedFormation transformation;
    private RoundedFormation transformation1;
    private MultiTransformation<Bitmap> mation;
    private RequestOptions options;


    public FoundGoodsAdapter(Activity mContext, List<FoundGoodContentBeen.DataBean> list) {
        this.mList = list;
        this.mContext = mContext;

        //顶部左边圆角
        transformation = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_LEFT);
        //顶部右边圆角
        transformation1 = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_RIGHT);
        //组合各种Transformation,
        mation = new MultiTransformation<>
                //Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法,将new CenterCrop()添加至此
                (new CenterCrop(), transformation, transformation1);
        options = RequestOptions.bitmapTransform(mation).override(100 * proportion, 100);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
    }

    public void setData() {
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_found_goods, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {

        holder.ttvContent.setText(mList.get(position).getTitle());
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(mation)).into(holder.ivFoundGood);
        if (UtilsData.getPreference(mContext, "activity").getBoolean("is_69")&&!SHOPID_69_NO.contains(mList.get(position).getStore_id())) {
            holder.tvHomeManJian.setVisibility(View.VISIBLE);
        } else {
            holder.tvHomeManJian.setVisibility(View.GONE);
        }
        if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
            holder.tvPrice.setText("¥" + mList.get(position).getMarketprice());
            holder.tvOldPrice.setVisibility(View.GONE);
        } else {
            holder.tvPrice.setText("¥" + mList.get(position).getDiscountprice());
            holder.tvOldPrice.setVisibility(View.VISIBLE);
            holder.tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            holder.tvOldPrice.setText("¥" + mList.get(position).getMarketprice());
        }
        holder.tvSaleNum.setText("已售" + mList.get(position).getSale_num() + "件");
        RoundedCorners roundedCorners = new RoundedCorners(20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getStore_logo()).apply(options.transform(new CircleCrop(), roundedCorners)).into(holder.ivShop);

        holder.tvShopName.setText(mList.get(position).getStore_name());
        holder.tvShopBase.setText("基础运费¥"+mList.get(position).getRetail_postage());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });
    }


    @Override
    public int getItemCount() {

        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_content)
        TagTextView ttvContent;
        @BindView(R.id.iv_found_good)
        ImageView ivFoundGood;
        @BindView(R.id.iv_shop)
        ImageView ivShop;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_old_price)
        TextView tvOldPrice;
        @BindView(R.id.tv_sale_num)
        TextView tvSaleNum;
        @BindView(R.id.tv_home_man_jian)
        TextView tvHomeManJian;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_shop_base)
        TextView tvShopBase;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
